package com.awanp3.nimbus.common.security.aspect;

import com.awanp3.nimbus.common.core.exception.ServiceException;
import com.awanp3.nimbus.common.security.annotation.ForbiddenInDemo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

/**
 * 演示模式禁用 api 切面
 *
 * @author 20014170
 */
@Aspect
@Slf4j
@RequiredArgsConstructor
@AutoConfiguration
@ConditionalOnProperty(name = "spring.security.demo.enabled", havingValue = "true", matchIfMissing = true)
public class DemoModeAspect {

    @Before(value = "@annotation(forbiddenInDemo)")
    public void beforeDisableApi(ForbiddenInDemo forbiddenInDemo) {
        String message = forbiddenInDemo.message();
        throw new ServiceException(message);
    }

}
